package com.xliee.modules.sys.controller;

import com.xliee.modules.sys.entity.UserEntity;
import com.xliee.modules.sys.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class LoginController {

    @Autowired
    IUserService userService;

    /**
     * 根-跳转
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "redirect:/index";
    }


    /**
     * 跳转到登录页
     *
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String toLogin(Model model) {
        model.addAttribute("user", new UserEntity());
        return "login";
    }

    /**
     * 登录实现
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(UserEntity user , HttpServletRequest request,Model model) {
        String str = userService.doLogin(user,request);
        if(str.equals("success")) {
            return "redirect:/index";
        }else {
            model.addAttribute("user", user);
            model.addAttribute("remind", "* "+str);
            return "login";
        }
    }


    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        if(request==null){
            return "redirect:/login";
        }
        int result = userService.logout(request);
        return "redirect:/login";
    }


    /**
     * 用户必读隐私手册
     *
     * @param model
     * @return
     */
    @RequestMapping("/readme")
    public String readme(Model model) {
        return "readme";
    }


    /**
     * 忘记密码
     * @param model
     * @return
     */
    @RequestMapping("/forgetPass")
    public String forgetPass(Model model) {
        return "forgetPass";
    }


}
